package com.atshiyou.servlet;

import com.atshiyou.webDatabase.ConnectMysql;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

@WebServlet(value = "/download")
public class DownloadServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        String username = (String) req.getSession().getAttribute("username");
        System.out.println("需要充值的账户:"+username);
        int score = 0;
        try {
            score = ConnectMysql.findPoint(username);
            System.out.println("查询出来的账户积分:"+score);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        if (score <= 100){
            //会员积分不够100
            PrintWriter printWriter = resp.getWriter();
            printWriter.write("<script>alert('会员积分不足，请充值！');location.href='./success.jsp'</script>");
        }else{
            PrintWriter printWriter = resp.getWriter();
            req.getSession().setAttribute("releasePoint",score);
            printWriter.write("<script>alert('恭喜您下载成功');location.href='./successdownload.jsp'</script>");
        }
    }
}
